<?php

namespace app\controller;

use app\BaseController;
use think\Request;
use app\service\RoleService;
use Exception;

class Role extends BaseController
{    
    protected $middleware = [
        \app\middleware\LoginCheck::class
    ];
    protected $request;
    protected $roleService;
    public function __construct(Request $request,RoleService $roleService)
    {       
        $this->request = $request;
        $this->roleService = $roleService;
    }    

    public function list()
    {       
        try {            
            $data = $this->roleService->list();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function purviews()
    {       
        try {    
            $params = $this->request->param();          
            $data = $this->roleService->rolePurviews($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function save()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->roleService->save($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function delete()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->roleService->delete($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function save_purview()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->roleService->saveRolePurviews($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }
}
